##################################################################################
##                                                                              ##
##  This file is part of the buola project (https://code.google.com/p/buola/).  ##
##                                                                              ##
##  Copyright(c) 2007-2012 Xavi Gratal                                          ##
##  gratal AT gmail DOT com                                                     ##
##                                                                              ##
##  Buola is free software: you can redistribute it and/or modify               ##
##  it under the terms of the GNU General Public License as published by        ##
##  the Free Software Foundation, either version 3 of the License, or           ##
##  (at your option) any later version.                                         ##
##                                                                              ##
##  Buola is distributed in the hope that it will be useful,                    ##
##  but WITHOUT ANY WARRANTY; without even the implied warranty of              ##
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               ##
##  GNU General Public License for more details.                                ##
##                                                                              ##
##  You should have received a copy of the GNU General Public License           ##
##  along with buola.  If not, see <http://www.gnu.org/licenses/>.              ##
##                                                                              ##
##################################################################################

macro(glsl_to_header glsl_target_name glsl_namespace)
    set(shaders_include_dir ${CMAKE_CURRENT_BINARY_DIR}/include/shaders)

    file(MAKE_DIRECTORY ${shaders_include_dir})
    include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)

    set(glsl_header_files)

    foreach(glsl_file ${ARGN})
        get_filename_component(name_only ${glsl_file} NAME_WE)
        add_custom_command(OUTPUT ${shaders_include_dir}/${name_only}.h
                        COMMAND ${CMAKE_BINARY_DIR}/apps/bin2hdr/bin2hdr --text --namespace ${glsl_namespace} --name ${name_only} ${glsl_file}
                                ${shaders_include_dir}/${name_only}.h
                        DEPENDS ${glsl_file} bin2hdr
                        COMMENT "creating header from glsl file (${name_only})"
                        VERBATIM)
        set(glsl_header_files ${glsl_header_files} ${shaders_include_dir}/${name_only}.h)
    endforeach()

    add_custom_target(${glsl_target_name} SOURCES ${glsl_header_files})
endmacro()

macro(glsl_to_header_folder dependent_target_name glsl_folder glsl_namespace)
    file(GLOB glsl_sources ${glsl_folder}/*.glsl ${glsl_folder}/*.vert ${glsl_folder}/*.frag)
    glsl_to_header(${dependent_target_name}_glsl_target ${glsl_namespace} ${glsl_sources})
    add_dependencies(${dependent_target_name} ${dependent_target_name}_glsl_target)
endmacro()